<template>
  <div class="search-top">
    <div class="search-box">
      <img src="../assets/back.png" alt="" @click="$router.go(-1)">
      <!-- Todo: 点击事件，回车搜索该歌曲 -->
      <input type="text" placeholder="搜索歌曲、歌手、专辑" v-model="currentSearchWord" @keyup.enter="setLocalItem">
    </div>
    <div class="history" v-show="!currentSearchWord">
      <div class="title">历史</div>
      <div class="list">
        <!-- Todo: 点击事件，点击搜索该歌曲 -->
        <div class="item" v-for="item in state.historySearch" :key="item" @click="$router.push({path: 'searchdetail', query:{keywords: item}})">{{item}}</div>
      </div>
      <div class="delete" @click="deleteHistory">
        <img src="../assets/delete.png" alt="">
      </div>
    </div>
  </div>
  <div class="hot-search" v-show="!currentSearchWord">
    <div class="hot-title">热门搜索</div>
    <!-- Todo: 点击事件，点击搜索该歌曲 -->
    <div class="hot-list">
      <div class="hot-item" v-for="item in state.hotList" :key="item.first" @click="$router.push({path: 'searchdetail', query:{keywords: item.first}})">{{ item.first }}</div>
    </div>
  </div>
  <div class="search-suggest" v-show="currentSearchWord">
    <div class="suggest-title">搜索"{{currentSearchWord}}"</div>
    <div class="suggest-list">
      <div class="suggest-item" v-for="item in state.allMatch" :key="item.keyword" @click="$router.push({path: 'searchdetail', query:{keywords: item.keyword}})">
        <img src="../assets/search.png" alt="" class="search-icon">
        <span class="search-content">{{item.keyword}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCurrentInstance, reactive, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { handleLocalStorage } from '../utils/handleLocalStorage.js'
import { debounce } from '../utils/debounce.js'

export default {
  setup() {
    const { proxy } = getCurrentInstance()
    const state = reactive({ hotList: [], historySearch: [], allMatch: [] })
    const currentSearchWord = ref()
    // 路由
    const router = useRouter()

    // 请求搜索热词
    proxy.$http.get('/search/hot').then(res => {
      if (res.code === 200) {
        state.hotList = res.result.hots
      }
    })

    // 初始化，获取localStorage数据
    ;(() => {
      const localDataString = handleLocalStorage('get', 'historySearch') || JSON.stringify([])
      state.historySearch = JSON.parse(localDataString)
    })()

    // 监听搜索词的改变，加入防抖
    function getSuggestRes() {
      const [newVal] = [...arguments]
      if (newVal) {
        proxy.$http.get(`/search/suggest?keywords=${newVal}&type=mobile`).then(res => {
          state.allMatch = res.result.allMatch
        })
      }
    }
    watch(currentSearchWord, debounce(getSuggestRes, 300))

    // 回车事件，设置本地存储
    const setLocalItem = () => {
      if (state.historySearch.indexOf(currentSearchWord.value) === -1) {
        state.historySearch.unshift(currentSearchWord.value)
        const historyString = JSON.stringify(state.historySearch)
        // currentSearchWord.value = ''
        handleLocalStorage('set', 'historySearch', historyString)
      } else {
        // currentSearchWord.value = ''
      }
      // 回车之后添加路由
      router.push({
        path: 'searchdetail',
        query: {
          keywords: currentSearchWord.value
        }
      })
      // 事件结束，清除currentSearchWord
      currentSearchWord.value = ''
    }

    // 点击事件，移除本地存储
    const deleteHistory = () => {
      handleLocalStorage('remove', 'historySearch')
      state.historySearch = []
    }

    return {
      state,
      currentSearchWord,
      setLocalItem,
      deleteHistory
    }
  }
}
</script>

<style lang="less" scoped>
.search-top {
  width: 7.5rem;
  .search-box {
    width: 7.5rem;
    display: flex;
    padding: 0.2rem;
    img {
      width: 0.8rem;
      height: 0.8rem;
    }
    input {
      flex: 1;
      font-size: 0.3rem;
      border: none;
      outline: none;
      border-bottom: 2px solid #ccc;
      margin-left: 0.2rem;
    }
  }
  .history {
    display: flex;
    justify-content: space-between;
    font-size: 0.3rem;
    padding: 0.2rem;
    .title {
      width: 1.2rem;
      font-weight: 600;
    }
    .list {
      height: 1rem;
      overflow: auto;
      display: flex;
      flex-wrap: wrap;
      .item {
        height: 0.5rem;
        line-height: 0.5rem;
        margin: 0 0.1rem 0.1rem;
        padding: 0 0.1rem;
        border-radius: 0.2rem;
        background-color: #ccc;
      }
    }
    .delete {
      img {
        width: 0.5rem;
        height: 0.5rem;
      }
    }
  }
}
.hot-search {
  font-size: 0.3rem;
  .hot-title {
    margin-left: 0.1rem;
    color: #666;
  }
  .hot-list {
    display: flex;
    flex-wrap: wrap;
    padding: 0 0.2rem;
    .hot-item {
      line-height: 0.5rem;
      font-size: 0.4rem;
      padding: 0 0.2rem;
      margin-top: 0.2rem;
      margin-right: 0.2rem;
      border: 1px solid #666;
      border-radius: 0.25rem;
    }
  }
}
.search-suggest {
  width: 7.5rem;
  font-size: 0.3rem;
  padding: 0.2rem;
  .suggest-title {
    height: 0.8rem;
    color: blue;
    margin: 0.2rem 0;
    border-bottom: 1px solid #ccc;
  }
  .suggest-list {
    .suggest-item {
      height: 1rem;
      display: flex;
      align-items: center;
      font-size: 0.4rem;
      border-bottom: 1px solid #ccc;
      .search-icon {
        width: 0.6rem;
        height: 0.6rem;
        margin-right: 0.2rem;
      }
    }
  }
}
</style>